package com.example.toutiao.ui.fragment;

import static android.content.Context.MODE_PRIVATE;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.toutiao.R;
import com.example.toutiao.adapter.ArticleAdapter;
import com.example.toutiao.db.Article;
import com.example.toutiao.db.DatabaseHelper;

import java.util.ArrayList;
import java.util.List;

//头条模块
public class HeadlinesFragment extends Fragment {

    private RecyclerView recyclerView;
    private List<Article> articleList;
    private ArticleAdapter adapter;
    private DatabaseHelper dbHelper;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_headlines, container, false);
        SharedPreferences sharedPreferences = getContext().getSharedPreferences("user_info", MODE_PRIVATE);
        String currentUsername = sharedPreferences.getString("username", null);
        recyclerView = view.findViewById(R.id.recycler_articles);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));

        dbHelper = new DatabaseHelper(requireContext());
        articleList = dbHelper.getArticlesByUsername(currentUsername); // 从数据库获取所有文章

        adapter = new ArticleAdapter(requireContext(), articleList);
        recyclerView.setAdapter(adapter);

        return view;
    }
}


